package com.grass.p019lv.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Process;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import androidx.exifinterface.media.ExifInterface;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.arouter.utils.Consts;

import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.NoticeBean;
import com.androidx.p013lv.base.bean.event.AppLinkEvent;
import com.androidx.p013lv.base.glide.GlideUtils;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.ActivityUtil;
import com.androidx.p013lv.base.utils.DownloadFileUtils;
import com.androidx.p013lv.base.utils.PackageUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.androidx.p013lv.base.utils.UiUtils;
import com.androidx.p013lv.mine.bean.DownLoadBean;
import com.androidx.p013lv.mine.bean.VersionBean;
import com.androidx.p013lv.mine.dialog.DialogUpdate;
import com.androidx.p013lv.mine.dialog.NoticeDialog;
import com.androidx.p013lv.mine.model.VersionUpdateModel;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.grass.p019lv.App;
import com.grass.p019lv.R;
import com.grass.p019lv.databinding.ActivityMainBinding;
import com.grass.p019lv.event.VoicePlayStatusEvent;
import com.grass.p019lv.fragment.FindFragment;
import com.grass.p019lv.fragment.HomeFragment;
import com.grass.p019lv.fragment.MineFragment;
import com.grass.p019lv.fragment.PiazzaAppointmentFragment;
import com.grass.p019lv.game.GameFragment;
import com.grass.p019lv.novel.activity.VoiceNovelActivityNew;
import com.grass.p019lv.utils.NotificationChannelHelper;
import com.lzy.okgo.model.Progress;
import com.shuyu.gsyvideoplayer.GSYVideoManager;

import java.io.File;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/* renamed from: com.grass.lv.activity.MainActivity */
public class MainActivity extends BaseActivity<ActivityMainBinding> implements DialogUpdate.VersionUpdateInterface {
    private int currentIndex = 0;
    DialogUpdate dialogUpdate;
    private String downLoadUrl;
    long exitTime;
    private FragmentManager fragmentManager;
    private SparseArray<LazyFragment> fragments = new SparseArray<>();
    private HomeFragment homeFragment;
    private VersionUpdateModel model;
    private RotateAnimation rotate;
    private VersionBean versionBean;

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return R.layout.activity_main;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void protectApp() {
        ARouter.getInstance().build(Constance.SPLASH_ACTIVITY).navigation();
        finish();
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        EventBus.getDefault().register(this);
        this.homeFragment = HomeFragment.newInstance();
        ((ActivityMainBinding) this.binding).setMain(this);
        ((ActivityMainBinding) this.binding).setPosition(Integer.valueOf(this.currentIndex));
        update();
        ((ActivityMainBinding) this.binding).ivSvip.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.MainActivity.View$OnClickListenerC28731 */

            public void onClick(View view) {
                MainActivity.this.startActivity(new Intent(MainActivity.this, SvipActivity.class));
            }
        });
        NotificationChannelHelper.initChannel(this);
        this.rotate = new RotateAnimation(0.0f, 360.0f, 1, 0.5f, 1, 0.5f);
        this.rotate.setInterpolator(new LinearInterpolator());
        this.rotate.setDuration(AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS);
        //DEFAULT_MIN_TIME_BETWEEN_BUFFER_REEVALUTATION_MS
        this.rotate.setRepeatCount(-1);
        this.rotate.setFillAfter(true);
        this.rotate.setStartOffset(10);
        ((ActivityMainBinding) this.binding).ivVoicePlayBtn.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.activity.MainActivity.View$OnClickListenerC28742 */

            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, VoiceNovelActivityNew.class);
                intent.putExtra(Key.NOVEL_ID, App.novelId);
                MainActivity.this.startActivity(intent);
            }
        });
        if (App.gameSwitch) {
            ((ActivityMainBinding) this.binding).llGame.setVisibility(View.VISIBLE);
        } else {
            ((ActivityMainBinding) this.binding).llGame.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void likeVideoEvent(AppLinkEvent appLinkEvent) {
        ActivityUtil.getInstance().finishAllActivityExcept(MainActivity.class);
        mainTabOnClickListener(4);
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initData() {
        super.initData();
        this.fragmentManager = getSupportFragmentManager();
        this.fragments.append(0, this.homeFragment);
        this.fragments.append(1, FindFragment.newInstance());
        this.fragments.append(3, MineFragment.newInstance());
        this.fragments.append(4, PiazzaAppointmentFragment.newInstance());
        this.fragments.append(5, GameFragment.newInstance());
        this.fragmentManager.beginTransaction().add(R.id.main_frame_layout, this.homeFragment).commitAllowingStateLoss();
    }

    public void mainTabOnClickListener(int i) {
        ((ActivityMainBinding) this.binding).setPosition(Integer.valueOf(i));
        showFragment(i);
    }

    private void showFragment(int i) {
        if (this.currentIndex != i) {
            LazyFragment lazyFragment = this.fragments.get(i);
            LazyFragment lazyFragment2 = this.fragments.get(this.currentIndex);
            FragmentTransaction beginTransaction = this.fragmentManager.beginTransaction();
            if (!lazyFragment.isAdded()) {
                beginTransaction.add(R.id.main_frame_layout, lazyFragment).hide(lazyFragment2);
            } else {
                beginTransaction.show(lazyFragment).hide(lazyFragment2);
            }
            beginTransaction.setMaxLifecycle(lazyFragment, Lifecycle.State.RESUMED);
            beginTransaction.setMaxLifecycle(lazyFragment2, Lifecycle.State.STARTED);
            beginTransaction.commitAllowingStateLoss();
            this.currentIndex = i;
        }
    }

    @Override // androidx.activity.ComponentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onBackPressed() {
        if (GSYVideoManager.isFullState(this)) {
            GSYVideoManager.backFromWindowFull(getActivity());
        } else if (System.currentTimeMillis() - this.exitTime > 1000) {
            this.exitTime = System.currentTimeMillis();
            ToastUtils.getInstance().showSigh("再按壹次退出");
        } else {
            Process.killProcess(Process.myPid());
            System.exit(1);
            super.onBackPressed();
        }
    }

    /* access modifiers changed from: package-private */
    public void update() {
        VersionUpdateModel versionUpdateModel = new ViewModelProvider(this).get(VersionUpdateModel.class);
        this.model = versionUpdateModel;
        versionUpdateModel.getVersionDate().observe(this, new Observer<BaseRes<VersionBean>>() {
            /* class com.grass.p019lv.activity.MainActivity.C28753 */

            public void onChanged(BaseRes<VersionBean> baseRes) {
                if (baseRes.getCode() == 200 && baseRes.getData() != null) {
                    MainActivity.this.versionBean = baseRes.getData();
                    MainActivity mainActivity = MainActivity.this;
                    mainActivity.versionUpdateDataProcessing(mainActivity.versionBean);
                }
            }
        });
        this.model.getDownLoadData().observe(this, new Observer<DownLoadBean>() {
            /* class com.grass.p019lv.activity.MainActivity.C28764 */

            public void onChanged(DownLoadBean downLoadBean) {
                MainActivity.this.versionUpdateUI(downLoadBean);
            }
        });
        this.model.data().observe(this, new Observer<BaseRes<NoticeBean>>() {
            /* class com.grass.p019lv.activity.MainActivity.C28775 */

            public void onChanged(BaseRes<NoticeBean> baseRes) {
                if (baseRes.getCode() != 200 || baseRes.getData() == null) {
                    MainActivity.this.homeFragment.getActivityInto();
                    MainActivity.this.homeFragment.getDeductData();
                    return;
                }
                MainActivity.this.showNoticeDialog(baseRes.getData());
            }
        });
        this.model.checkLatestVersion();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void versionUpdateDataProcessing(VersionBean versionBean2) {
        Context activity = getActivity();
        if (activity != null) {
            try {
                if (!TextUtils.isEmpty(versionBean2.getVersionNum()) && Integer.parseInt(PackageUtils.getVersionName(activity).replace(Consts.DOT, "")) < Integer.parseInt(versionBean2.getVersionNum().replace(Consts.DOT, ""))) {
                    this.downLoadUrl = versionBean2.getLink();
                    String str = ExifInterface.GPS_MEASUREMENT_INTERRUPTED + versionBean2.getVersionNum();
                    if (versionBean2.isIsForceUpdate()) {
                        this.dialogUpdate = new DialogUpdate(this, str, versionBean2.getInfo(), versionBean2.getLink(), true, this);
                    } else {
                        this.dialogUpdate = new DialogUpdate(this, str, versionBean2.getInfo(), versionBean2.getLink(), false, this);
                    }
                    this.dialogUpdate.show();
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.model.getNoticeData();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showNoticeDialog(NoticeBean noticeBean) {
        NoticeDialog noticeDialog = new NoticeDialog(getActivity(), noticeBean);
        noticeDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            /* class com.grass.p019lv.activity.MainActivity.DialogInterface$OnDismissListenerC28786 */

            public void onDismiss(DialogInterface dialogInterface) {
                MainActivity.this.homeFragment.getActivityInto();
                MainActivity.this.homeFragment.getDeductData();
            }
        });
        noticeDialog.show();
    }

    @Override // com.androidx.p013lv.mine.dialog.DialogUpdate.VersionUpdateInterface
    public void onConfirmClick() {
        if (TextUtils.isEmpty(this.downLoadUrl)) {
            this.dialogUpdate.dismiss();
            ToastUtils.getInstance().showWeak("無效下載鏈接");
        } else if (this.downLoadUrl.startsWith("http")) {
            this.model.doDownLoadFile(this.downLoadUrl);
        } else {
            this.dialogUpdate.dismiss();
            ToastUtils.getInstance().showWeak("下載鏈接非法");
        }
    }

    @Override // com.androidx.p013lv.mine.dialog.DialogUpdate.VersionUpdateInterface
    public void dismissDialog() {
        this.model.getNoticeData();
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void versionUpdateUI(DownLoadBean downLoadBean) {
        if (downLoadBean != null && this.dialogUpdate != null) {
            if (downLoadBean.getCode() == 0) {
                this.dialogUpdate.startDownLoad();
                return;
            }
            Progress progress = downLoadBean.getProgress();
            if (downLoadBean.getCode() == 1) {
                this.dialogUpdate.setProgressState(progress);
            } else if (downLoadBean.getCode() == 2) {
                this.dialogUpdate.dismiss();
                ToastUtils.getInstance().showWrong(UiUtils.getString(R.string.hit_no_net));
            } else if (downLoadBean.getCode() == 3) {
                File file = downLoadBean.getFile();
                this.dialogUpdate.dismiss();
                Context activity = getActivity();
                if (activity != null) {
                    DownloadFileUtils.openFile(activity, file);
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onPause() {
        super.onPause();
        GSYVideoManager.onPause();
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(VoicePlayStatusEvent voicePlayStatusEvent) {
        if (((ActivityMainBinding) this.binding).ivVoicePlayBtn != null) {
            if (!TextUtils.isEmpty(App.floatingCoverImg)) {
                GlideUtils.loadCropRadiusWh2(App.floatingCoverImg, 1, ((ActivityMainBinding) this.binding).ivVoicePlayBtn, Constance.BIG_PIC);
            } else {
                GlideUtils.loadCropRadiusDefault(1, ((ActivityMainBinding) this.binding).ivVoicePlayBtn);
            }
            if (voicePlayStatusEvent.isVoicePlayStatus()) {
                ((ActivityMainBinding) this.binding).ivVoicePlayBtn.setVisibility(0);
                ((ActivityMainBinding) this.binding).ivVoicePlayBtn.startAnimation(this.rotate);
                return;
            }
            ((ActivityMainBinding) this.binding).ivVoicePlayBtn.clearAnimation();
            ((ActivityMainBinding) this.binding).ivVoicePlayBtn.setVisibility(8);
        }
    }
}
